package com.pinda.dao;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import com.pinda.dao.vo.ReturnLocationBean;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class AddressToCoordinate {
    /**
     * http://lbsyun.baidu.com/apiconsole/key
     * <百度开发者>用户申请注册的key，自v2开始参数修改为“ak”，之前版本参数为“key” 申请ak
     */
    final static String AK = "xMHTAOB69B9mOlSbszgrm157MEzwsk5G";


    /**
     * 地理编码 URL
     */
    final static String ADDRESS_TO_LONGITUDEA_URL = "http://api.map.baidu.com/geocoding/v3/?output=json&location=showLocation";


    public ReturnLocationBean getReturnLocationBean(String address){
//        String address="广东省广州市黄埔区";
        if(StringUtils.isBlank(address)){
    //            return null;
        }
        String url = ADDRESS_TO_LONGITUDEA_URL + "&ak=" + AK + "&address="+ address;
    //        log.info("请求url:" + url);
        HttpClient client = HttpClients.createDefault(); // 创建默认http连接
        HttpPost post = new HttpPost(url);// 创建一个post请求
        try {
            HttpResponse response = client.execute(post);// 用http连接去执行get请求并且获得http响应
            HttpEntity entity = response.getEntity();// 从response中取到响实体
            String html = EntityUtils.toString(entity);// 把响应实体转成文本
    //            log.info("返回信息：" + html);
            // JSON转对象
            System.out.println(html);
            Gson gson = new Gson();
            ReturnLocationBean o = gson.fromJson(html, ReturnLocationBean.class);
            return o;
        } catch (Exception e) {
    //            log.error("地理编码[异常],", e);

        }
        return null;
    }


}